package com.ws.wsdemo2.server;

import com.alibaba.fastjson.JSONObject;
import com.sun.org.apache.xpath.internal.operations.Quo;
import com.ws.wsdemo2.model.Quote;
import com.ws.wsdemo2.service.IComputeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPubSub;
import com.ws.wsdemo2.util.QuotePeriodUtil.PeriodType;

import java.io.IOException;

/**
 * @Classname RedisSubscriber
 * @Description TODO
 * @Date 2019/5/9 17:13
 * @Created by Longer
 */
@Component
public class RedisSubscriber extends JedisPubSub {

    @Autowired
    WebSocketServer webSocketServer;

    @Autowired
    JedisPool jedisPool;
    
    @Autowired
    IComputeService computeService;
    
    public RedisSubscriber() {
    }

    public void onMessage(String channel, String message) {
        Jedis jedis = jedisPool.getResource();
        try {
            System.out.println(String.format("订阅到消息, channel %s, message %s", channel, message));
            doOnMessage(message);
            //向所有用户推消息
            webSocketServer.sendInfos(message);
        } catch (Exception e) {
            //容错机制
            jedis.lpush("test:list",message);
            e.printStackTrace();
        }
    }

    public void onSubscribe(String channel, int subscribedChannels) {
        System.out.println(String.format("subscribe redis channel success, channel %s, subscribedChannels %d",
                channel, subscribedChannels));
    }

    public void onUnsubscribe(String channel, int subscribedChannels) {
        System.out.println(String.format("unsubscribe redis channel, channel %s, subscribedChannels %d",
                channel, subscribedChannels));

    }

    public void doOnMessage(String msg) throws Exception {
        int count=0;
        Jedis jedis = jedisPool.getResource();
        //先检查队列里面是否有没消费的信息
        while (jedis.llen("test:list")!=0){
            String ms = jedis.lpop("test:list");
            System.out.println(ms);
            webSocketServer.sendInfo(2L,ms);
        }


        Quote quote = JSONObject.parseObject(msg, Quote.class);
        //将最新行情存放到redis
        String lastKey="QUOTESERVICE:LAST_MARKET_DATA:"+quote.getProductTradeCode()+"";
        JSONObject marketData = new JSONObject();
        marketData.put("PRODUCTTRADECODE", quote.getProductTradeCode());//商品标识
        marketData.put("PRICE", quote.getPrice());//成交价格
        marketData.put("DEALCOUNT",quote.getDealcount());//成交量
        marketData.put("DEALAMOUNT",quote.getDealamount());//成交量
        marketData.put("DEALTIME", quote.getDealtime());//成交时间
        jedis.set(lastKey,marketData.toJSONString());
        // K线数据统计
        computeService.statistical(jedis, count, PeriodType.ONE_MINUTE, marketData);
        computeService.statistical(jedis, count, PeriodType.FIVE_MINUTE, marketData);
        computeService.statistical(jedis, count, PeriodType.FIFTEEN_MINUTE, marketData);
        computeService.statistical(jedis, count, PeriodType.THIRTY_MINUTE, marketData);
        computeService.statistical(jedis, count, PeriodType.SIXTY_MINUTE, marketData);
        /*computeService.statistical(jedis, count, PeriodType.FOUR_HOUR, marketData);*/
        computeService.statistical(jedis, count, PeriodType.DAY, marketData);
        computeService.statistical(jedis, count, PeriodType.WEEK, marketData);
        computeService.statistical(jedis, count, PeriodType.MONTH, marketData);
    }
}

